
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class ErrorItem(QGraphicsTextItem):
    def __init__(self,parent,exc,scene):
        QGraphicsTextItem.__init__(self,"Error",parent)
        self.exc = exc
        self.setDefaultTextColor(QColor(Qt.red))
        self.setPos(-20,-20)
        self.scene = scene

    def mousePressEvent(self,event):
        event.accept()
        if not self.errorBox():
            return
        for i,e in enumerate(self.scene.errors):
            if e is self:
                index = i
        del self.scene.errors[index]
        self.deleteLater()
        

    def errorBox(self):
        msgBox = QMessageBox(None)
        clearButton = msgBox.addButton("Clear", QMessageBox.ActionRole)
        cancelButton = msgBox.addButton(QMessageBox.Cancel)

        msgBox.setText(str(self.exc))

        msgBox.exec_();

        if msgBox.clickedButton() == clearButton:
            return True
        else:
            return False
